﻿<!DOCTYPE html>
<html>
<head>
    <title>所有链接</title>
    $css() 
    $iconcss()
    <style type="text/css">
        .ui-datagrid-header{display: none;}
        .ui-datagrid-view td, .ui-datagrid-header td{ border-left: none;}
    </style>
</head>
<body>
    
<table cellpadding="0" cellspacing="0" width="100%" height="100%">
                <tr>
                    <td width="350" valign="top" class="control">
                        <div class="top">
                            <div class="list-menu" id="list-menu">
                                <span class="icon"></span>菜单
                                <div class="drop">
                                    <a href="javascript:;" class="fn item" fn="create">$icon(create)创建文档关联</a>
                                    <a href="javascript:;" class="fn item last" fn="refresh">$icon(refresh)刷新列表</a>
                                </div>
                            </div>


                        </div>

                        <div id="dg"></div>
                        <div class="ui-table-meta" id="pager"></div>
                    </td>
                    <td valign="top">
                        <div id="pl" class="relative autoHeight autoScroll"></div>
                    </td>
                </tr>
            </table>

<script type="text/template" id="link_form">
            
                    <div class="form" style="padding:2em;">

                        <div class="title" style="margin-top: 0">
                            <span class="icon icon1"></span>{form_title}
                        </div>
                        <div class="fl">
                            <div class="label">
                                <span class="red">*&nbsp;</span>关联类型：</div>
                            <div class="in">
                                <select name="RelatedIndent" class="ui-box" field="RelatedIndent" onchange="relatedIndentChanged.apply(this)">
                                    <option value="">-- 请选择 --</option>
                                    ${indent_opts}
                                </select>
                            </div>
                        </div>

                        <div class="fl">
                            <div class="label">
                                <span class="red">*&nbsp;</span>链接文档：
                            </div>
                            <div class="in">
                                <input class="ui-validate ui-box" type="hidden" field="Id" value="0" />
                                <input class="ui-validate ui-box" type="hidden" field="ContentType" value="${content_type}" />
                                <input class="ui-validate ui-box" type="hidden" field="ContentId" value="${content_id}" />
                                <input class="ui-validate ui-box" type="hidden" field="RelatedSiteId" value="0" />
                                <input class="ui-validate ui-box" type="hidden" field="RelatedContentId" value="0" />
                                <input type="text" class="tb_normal ui-box archiveGuess" field="Title" />
                                <div>输入关键词后选择（注：类型可能不限站点）</div>
                                <span class="label hidden" id="lb_selected"></span>
                            </div>
                        </div>
                        
                        <div class="fl">
                            <div class="label">
                                <span class="red">*&nbsp;</span>是否启用：
                            </div>
                            <div class="in">
                                <input type="radio" field="Enabled" name="Enabled" value="1" id="enabled1" checked="checked"/><label for="enabled1">启用</label>
                                <input type="radio" field="Enabled" name="Enabled" value="0" id="enabled2" /><label for="enabled2">停用</label>
                            </div>
                        </div>
                        
                        <div class="fl">
                            <div class="label">&nbsp;</div>
                            <div class="in">
                                <span class="ui-button w150" id="btn" onclick="saveData()">
                                    <span class="button-inner">
                                        <span class="button-txt">{btntext}</span>
                                        <a href="javascript:;"></a>
                                    </span>
                                </span>
                            </div>
                        </div>
                </div>
        </script>

<script type="text/template" id="item_template" class="hidden">
        <div class="archive-item" valign="middle" style="overflow:hidden-x;width:100%">
            <span class="index">{index}</span>
            <span title="{title}">{title}</span>
            <br/>
            <span style="padding-left:2.2em">
                <span class="label">{type}</span>&nbsp;
                <span class="label">{siteName}</span>&nbsp;
                <a href="{url}" target="_blank" title="浏览关联文档">浏览</a> |
                <a href="javascript:;" onclick="Fn.edit('{id}')">修改</a> |
                <a href="javascript:;" onclick="Fn.del('{id}')">删除</a>
            </span>
        </div>
 </script>
$js()
    <script type="text/javascript">
        
        var dataCollection = null;
        var linkData = {
            ContentId: '${content_id}',
            ContentType: '${content_type}'
        };

        var pl = jr.$('pl');

        var listMenu = jr.$('list-menu');
        jr.hover(listMenu);

        var itemTemplate = jr.$('item_template').innerHTML;
        var lbSelected;

        var form_tpl = jr.$('link_form').innerHTML;

        var _dg = jr.datagrid('dg', {
            url: '?module=link&action=related_link',
            data: linkData,
            idField: 'Id',
            columns: [
                {
                    field: 'Id',
                    title: '',
                    formatter: function (val, row, index) {
                        return jr.template(itemTemplate, {
                            id: row.Id,
                            title: row.Title,
                            url:'${scheme}'+ row.Url.replace('#','${host}'),
                            type: row.IndentName,
                            siteName:row.RelatedSiteName,
                            index:index+1,
                        });
                    }
                }
            ],
            loaded: function (data) {
                dataCollection = data.rows;
                jr.each(jr.dom.getsByClass(document.body, 'ui-datagrid-view'), function (i, e) {
                    var tbs = e.getElementsByTagName('TABLE');
                    if (tbs.length != 0) {
                        tbs[0].className = 'ui-table ui-table-list';
                    }
                });

                //加载分页
                this.panel.nextSibling.innerHTML = data.pager;

            }
        });




        window.saveData = function () {
            if (jr.validator.validate('pl')) {
                var data = jr.json.toObject('pl');

                if (data.RelatedIndent === '') {
                    showErr('请选择关联类型');
                    return false;
                }
                if (data.RelatedSiteId <= 0 || data.RelatedContentId <= 0) {
                    showErr('请选择关联文档');
                    return false;
                }


                jr.xhr.jsonPost('?module=link&action=save_related_link', data, function (json) {
                    if (json.result) {
                        showMsg2(json.message, function() { _dg.reload(); });
                    } else {
                        showErr(json.message);
                    }
                });
            }
        };


        Fn.edit = function (id) {
            var d;
            for (var i = 0; i < dataCollection.length; i++) {
                if (dataCollection[i].Id === parseInt(id)) {
                    d = dataCollection[i];
                    pl.innerHTML = jr.template(form_tpl, { btntext: '保存', form_title: '修改文档关联' });
                    jr.json.bind(pl, d);
                    initFormEvents('${site_id}','');
                    break;
                }
            }
        };


        function relatedIndentChanged() {
            if (this.value == '') {
                initFormEvents('${site_id}', '');
            }else{
            var selOpt = this.options[this.selectedIndex];
                var siteId = selOpt.getAttribute('site-lmt');
                var cateId = selOpt.getAttribute('cate-lmt');
                initFormEvents(siteId, cateId);
            }
        }


        var com;
        function initFormEvents(siteId,categoryId) {
            jr.each(jr.dom.getsByClass(jr.$('pl'), 'archiveGuess'), function (i, e) {
                var url = '?module=ajax&action=GetSearchArchivesJsonResult&size=10&only_title=true&site_id=' + siteId + '&category=' + categoryId;
                if (e.getAttribute('inited') == undefined) {
                  com =  jr.autoCompletion(e, url, function(data) {

                        jr.json.bind('pl', { RelatedContentId: 0 });
                        lbSelected = jr.$('lb_selected');
                        lbSelected.className = 'label hidden';

                        for (var j = 0; j < data.length; j++) {
                            data[j].text = data[j].title;
                            data[j].title = '站点：' + data[j].siteName + '  ** 栏目：' + data[j].category;
                        }
                    }, function(data) {
                        jr.json.bind('pl', { RelatedContentId: data.id, RelatedSiteId: data.siteId });
                        lbSelected.className = 'label';
                        lbSelected.innerHTML = '站点：' + data.siteName + '  ** 栏目：' + data.category;
                    }, function() {
                        closeMsg();
                    });

                    e.setAttribute('inited', '1');
                } else {
                    com.url = url;
                }
            });
        }

        Fn.create = function() {
            jr.$('pl').innerHTML = jr.template(form_tpl, { btntext: '提交',form_title:'创建文档关联' });
            jr.json.bind('pl', { Enabled: true });
            initFormEvents('${site_id}', '');
        };

        Fn.del = function(id) {
            if (confirm('确定删除吗?删除后将不可恢复!')) {
                var d = {
                    id: id,
                    contentId:linkData.ContentId,
                    contentType: linkData.ContentType
                };
                jr.xhr.jsonPost('?module=link&action=delete_related_link', d,
                    function(json) {
                        if (json.result) {
                            _dg.reload();
                        } else {
                            showErr(json.message);
                        }
                    });
            }
        };

        Fn.refresh = function() {
            _dg.reload();
        };


        Fn.create();
         
    </script>

</body>
</html>
